package divtastic.js.application.views;
import zpartanlite.Enumerables;
import divtastic.js.DisplayDiv;
import zpartanlite.Pages;
import zpartanlite.DispatchTo;
import feffects.Tween;

class SwipeView
{
    
    
    private var velocity:           Int;    
    public var index0:              Int;
    public var index1:              Int;
    
    private var _old:               DisplayDiv;
    private var _curr:              DisplayDiv;
    private var _oldContent:        DisplayDiv;
    private var _distance:          Int;
    private var pages:              Pages<String>;
    private var _direction:         Orientation;
    private var _enabled:           Bool;
    public var pageChange:          DispatchTo;
    
    
    // _oldcontent should be within _old for this class to work
    public function new(    direction_:     Orientation
                        ,   distance_:      Int
                        ,   curr_:          DisplayDiv
                        ,   old_:           DisplayDiv
                        ,   oldContent_:    DisplayDiv
                        ,   pages_:         Pages<String>
                        )
    {
        
        pageChange  = new DispatchTo();
        _direction  = direction_;
        _distance   = distance_;
        _curr       = curr_;
        _old        = old_;
        pages      = pages_;
        _oldContent = oldContent_;
        _curr.setClip();
        _old.setClip();
        _oldContent.setClip();
        _enabled    = true;
        velocity    = 500;
        pages.pageChange.add( setImage );
        
    }
    
    
    public var orientation( get_orientation, set_orientation ):Orientation;
    
    private function get_orientation():Orientation
    {
        
        return _direction;
        
    }
    
    public function set_orientation( val: Orientation ): Orientation
    {
        
        _direction = val;
        return val;
        
    }
    
    
    public var enabled( get_enabled, set_enabled ):Bool;
    
    private function get_enabled(): Bool
    {
        
        return _enabled;
        
    }
    
    private function set_enabled( val: Bool )
    {
        
        var was = _enabled;
        _enabled = val;
        
        if( _enabled && was != _enabled )
        {
            
            pages.pageChange.add( setImage );
            
        }
        else if( !_enabled && was != _enabled )
        {
            
            pages.pageChange.remove( setImage );
            
        }
        
        return _enabled;
        
    }
    
    
    public function setImage()
    {
        
        var old         = _old;
        var curr        = _curr;
        var ina         = _oldContent;
        var distance    = _distance;
        var direction   = _direction;
        var pages       = pages;
        
        curr.visible    = false;
        ina.visible     = false;
    
        var s:          Int;
        var f:          Int;
        
        if( pages.dir == Forward )
        {
        
            ina.setImage( pages.last );
            index0 = pages.getLastIndex();
            //innerTxt.updateText( imageText[  ] );
            curr.setImage( pages.curr );
            index1 = pages.getIndex();
            //imageHolderTxt.updateText( imageText[ pages.getIndex() ] );
            s = 0;
            f = distance;
        
        } 
        else
        {
            //innerTxt.updateText( imageText[ pages.getIndex() ] );
            index0 = pages.getIndex();
            ina.setImage( pages.curr );
            //imageHolderTxt.updateText( imageText[ pages.getLastIndex() ] );
            curr.setImage( pages.last );
            index1 = pages.getLastIndex();
            s = distance;
            f = 0;
        
        }
        
        pageChange.dispatch();
        
        if( direction == Horizontal )
        {
            curr.width       = s; 
            old.width       = distance - s;
            old.x           = curr.x + s; 
            ina.x           = -s;
        }
        else
        {
            curr.height      = s; 
            old.height       = distance - s;
            old.y           = curr.y + s; 
            ina.y           = -s;
        }
        
        curr.visible    = true;
        ina.visible     = true;
        
        var t           = new Tween( s, f, velocity );
        
        if( direction == Horizontal )
        {
            t.setTweenHandlers( function( e ){ 
                                                    curr.width   = e; 
                                                    old.width   = distance - e;
                                                    old.x       = curr.x + e; 
                                                    ina.x       = -e;
                                                }  );
        } 
        else
        {
            t.setTweenHandlers( function( e ){ 
                                                    curr.height     = e; 
                                                    old.height      = distance - e;
                                                    old.y           = curr.y + e; 
                                                    ina.y           = -e;
                                                }  );
        }
        
        t.start();
    
    }
    
}